package com.glueball.crm.module.client.ui;

import com.glueball.crm.module.client.model.Address;
import com.glueball.crm.module.common.ui.CrmWidget;
import com.glueball.crm.module.common.ui.EditableWidget.StartView;
import com.glueball.crm.module.common.ui.KeyValueWidget;
import com.glueball.crm.module.util.KeyValue;
import com.glueball.crm.module.util.URLHelper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class AddressWidget extends CrmWidget {
	
	@UiField SimplePanel countryPanel;
	@UiField HorizontalPanel countryCtrlPanel;
	@UiField SimplePanel cityPanel;
	@UiField HorizontalPanel cityCtrlPanel;
	@UiField SimplePanel postCodePanel;
	@UiField HorizontalPanel postCodeCtrlPanel;
	@UiField SimplePanel streetPanel;
	@UiField HorizontalPanel streetCtrlPanel;
	@UiField SimplePanel houseNumberPanel;
	@UiField HorizontalPanel houseNumberCtrlPanel;
	@UiField SimplePanel floorPanel;
	@UiField HorizontalPanel floorCtrlPanel;
	@UiField SimplePanel doorNumberPanel;
	@UiField HorizontalPanel doorNumberCtrlPanel;

	private final String saveUrl;
	private final Address entity;
	
	private KeyValueWidget countryWidget;
	private KeyValueWidget cityWidget;
	private KeyValueWidget postCodeWidget;
	private KeyValueWidget streetWidget;
	private KeyValueWidget houseNumberWidget;
	private KeyValueWidget floorWidget;
	private KeyValueWidget doorNumberWidget;
	
	private static AddressWidgetUiBinder uiBinder = GWT.create(AddressWidgetUiBinder.class);

	interface AddressWidgetUiBinder extends UiBinder<Widget, AddressWidget> {
	}

	public AddressWidget(final Address entity, final CrmWidget parent, final String clientId) {
		this.entity = entity;
		this.saveUrl = URLHelper.MODIFY_ADDRESS+clientId;
		this.widget = uiBinder.createAndBindUi(this);
		refresh();
	}

	public void refresh() {
			this.countryWidget = new KeyValueWidget(
				new KeyValue("country", this.entity.getCountry()),
				countryPanel,
				countryCtrlPanel,
				StartView.SHOW,
				this.saveUrl);

			this.cityWidget = new KeyValueWidget(
				new KeyValue("city", this.entity.getCity()),
				cityPanel,
				cityCtrlPanel,
				StartView.SHOW,
				this.saveUrl);

			this.postCodeWidget = new KeyValueWidget(
				new KeyValue("postCode", this.entity.getPostCode()),
				postCodePanel,
				postCodeCtrlPanel,
				StartView.SHOW,
				this.saveUrl);

			this.streetWidget = new KeyValueWidget(
				new KeyValue("street", this.entity.getStreet()),
				streetPanel,
				streetCtrlPanel,
				StartView.SHOW,
				this.saveUrl);

			this.houseNumberWidget = new KeyValueWidget(
				new KeyValue("houseNumber", this.entity.getHouseNumber()),
				houseNumberPanel,
				houseNumberCtrlPanel,
				StartView.SHOW,
				this.saveUrl);

			this.floorWidget = new KeyValueWidget(
				new KeyValue("floor", this.entity.getFloor()),
				floorPanel,
				floorCtrlPanel,
				StartView.SHOW,
				this.saveUrl);
			
			this.doorNumberWidget = new KeyValueWidget(
				new KeyValue("doorNumber", this.entity.getDoorNumber()),
				doorNumberPanel,
				doorNumberCtrlPanel,
				StartView.SHOW,
				this.saveUrl);
	}

	public KeyValueWidget getCountryWidget() {
		return countryWidget;
	}

	public void setCountryWidget(KeyValueWidget countryWidget) {
		this.countryWidget = countryWidget;
	}

	public KeyValueWidget getCityWidget() {
		return cityWidget;
	}

	public void setCityWidget(KeyValueWidget cityWidget) {
		this.cityWidget = cityWidget;
	}

	public KeyValueWidget getPostCodeWidget() {
		return postCodeWidget;
	}

	public void setPostCodeWidget(KeyValueWidget postCodeWidget) {
		this.postCodeWidget = postCodeWidget;
	}

	public KeyValueWidget getStreetWidget() {
		return streetWidget;
	}

	public void setStreetWidget(KeyValueWidget streetWidget) {
		this.streetWidget = streetWidget;
	}

	public KeyValueWidget getHouseNumberWidget() {
		return houseNumberWidget;
	}

	public void setHouseNumberWidget(KeyValueWidget houseNumberWidget) {
		this.houseNumberWidget = houseNumberWidget;
	}

	public KeyValueWidget getFloorWidget() {
		return floorWidget;
	}

	public void setFloorWidget(KeyValueWidget floorWidget) {
		this.floorWidget = floorWidget;
	}

	public KeyValueWidget getDoorNumberWidget() {
		return doorNumberWidget;
	}

	public void setDoorNumberWidget(KeyValueWidget doorNumberWidget) {
		this.doorNumberWidget = doorNumberWidget;
	}
}
